use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::mapper::ValueMapper;
use crate::modeling::{
    Class, ClassAttribute, ClassAttributeBuilder, ClassAttributePersistence, ClassBuilder,
    ClassGetter, ClassPersistence, ClassPersistenceBuilder, ClassRelation, ClassRelationGetter,
    ValueType,
};
use crate::modeling::{ClassPersistenceGetter, Value};

pub const DATASOURCE_CLASS_ATTRIBUTE_MAPPING_CLASS_IDENTIFIER: &'static str =
    "g01_datasource_class_attribute_mappings";

pub const DATASOURCE_CLASS_ATTRIBUTE_MAPPING_CLASS_NAME: &'static str =
    "datasource_class_attribute_mappings";

pub const DATASOURCE_CLASS_ATTRIBUTE_MAPPING_TABLE_NAME: &'static str =
    "_g01_meta_datasource_class_attribute_mappings";

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "DataSourceClassAttributeMappingBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct DataSourceClassAttributeMapping {
    /// id
    pub id: i64,
    /// id
    pub datasource_id: i64,
    /// identifier
    pub class_identifier: String,
    /// identifier
    pub class_attribute_identifier: String,
    /// 图标
    pub column_name: String,
    /// 选项
    pub options: Option<String>,
    /// 备注
    pub description: String,
    /// 创建时间
    pub created_at: i64,
    /// 更新时间
    pub updated_at: i64,
}

//
crate::impl_from_value!(DataSourceClassAttributeMapping);
crate::impl_class_persistence_getter!(
    DataSourceClassAttributeMapping,
    DATASOURCE_CLASS_ATTRIBUTE_MAPPING_TABLE_NAME
);
crate::impl_class_relations_getter!(DataSourceClassAttributeMapping, vec![]);

impl ClassGetter for DataSourceClassAttributeMapping {
    fn get_name() -> anyhow::Result<String> {
        Ok(String::from(DATASOURCE_CLASS_ATTRIBUTE_MAPPING_CLASS_NAME))
    }

    fn get_identifier() -> anyhow::Result<String> {
        Ok(String::from(
            DATASOURCE_CLASS_ATTRIBUTE_MAPPING_CLASS_IDENTIFIER,
        ))
    }
    fn get_class() -> anyhow::Result<Class> {
        let mut attribute_builder = ClassAttributeBuilder::default();
        attribute_builder.class_identifier(DATASOURCE_CLASS_ATTRIBUTE_MAPPING_CLASS_IDENTIFIER);

        let mut commons = super::common_attributes_with_builder(&attribute_builder)?;
        let mut append = vec![
            attribute_builder
                .clone()
                .name("datasource_id")
                .title("数据源")
                .data_type(ValueType::Number)
                .build()?,
            attribute_builder
                .clone()
                .name("class_identifier")
                .title("领域")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("class_attribute_identifier")
                .title("类型")
                .data_type(ValueType::String)
                .build()?,
            attribute_builder
                .clone()
                .name("column_name")
                .title("列名")
                .data_type(ValueType::String)
                .build()?,
        ];
        append.append(&mut commons);
        append.sort_by(|a, b| a.position.cmp(&b.position));
        let clazz = ClassBuilder::default()
            .identifier(DATASOURCE_CLASS_ATTRIBUTE_MAPPING_CLASS_IDENTIFIER)
            .name(DATASOURCE_CLASS_ATTRIBUTE_MAPPING_CLASS_NAME)
            .data_is_type(true)
            .is_persistent(true)
            .primary_key(String::from(
                "datasource_id,class_identifier,class_attribute_identifier",
            ))
            .attributes(append)
            .build()?;
        Ok(clazz)
    }
}
